layui.use(['table', 'admin', 'ax', 'func', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;

    /**
     * 管理
     */
    var CbAbnormalBack = {
        tableId: "cbAbnormalBackTable"
    };

    /**
     * 初始化表格的列
     */
    CbAbnormalBack.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'typeName', sort: true, title: '反馈类型'},
            {field: 'suggest', sort: true, title: '产品建议'},
            {field: 'contact', sort: true, title: '联系方式'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'createUser', sort: true, title: '创建人'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    CbAbnormalBack.search = function () {
        var queryData = {};
        var timeLimit = $('#timeLimit').val();
        if (timeLimit!=null && timeLimit!=""){
            var startDate = timeLimit.split(' - ')[0]+" 00:00:00";
            var endDate = timeLimit.split(' - ')[1]+" 23:59:59";
            queryData['startDate'] = startDate;
            queryData['endDate'] = endDate;
        }else {
            queryData['startDate'] = '';
            queryData['endDate'] = '';
        }

        queryData['id'] = $('#id').val();
        queryData['typeName'] = $('#typeName').val();

        table.reload(CbAbnormalBack.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    CbAbnormalBack.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/api/abnormal_back/add'
    };

    /**
    * 跳转到详情页面
    *
    * @param data 点击按钮时候的行数据
    */
    CbAbnormalBack.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/api/abnormal_back/detail_view?id=' + data.id
    };

    /**
     * 导出excel按钮
     */
    CbAbnormalBack.exportExcel = function () {
        var checkRows = table.checkStatus(CbAbnormalBack.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    CbAbnormalBack.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/api/abnormal_back/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(CbAbnormalBack.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + CbAbnormalBack.tableId,
        url: Feng.ctxPath + '/api/abnormal_back/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: CbAbnormalBack.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        CbAbnormalBack.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        CbAbnormalBack.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        CbAbnormalBack.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + CbAbnormalBack.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'detail') {
            CbAbnormalBack.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            CbAbnormalBack.onDeleteItem(data);
        }
    });

    function photograph(t) {
        console.log("photograph: "+t);
        var t = $(t).find("img");
        if(t == null||t == ''){
            return;
        }
        layer.open({
            type: 1,
            skin: 'layui-layer-rim', //加上边框
            area: ['75%', '85%'], //宽高
            shadeClose: true, //开启遮罩关闭
            end: function (index, layero) {
                return false;
            },
            content: '<div style="text-align:center"><img src="' + $(t).attr('src') + '" /></div>'
        });
    }

    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        max: Feng.currentDate()
    });
});
